<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">库存首页</el-breadcrumb-item>
      <el-breadcrumb-item>相关人员</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryInfo.name" clearable @clear="getList">
            <el-button slot="append" icon="el-icon-search" @click="getList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-select v-model="value" placeholder="请选择人员类型" @change="getListByType(value)">
            <el-option label="全部" value=""></el-option>
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加人员</el-button>
        </el-col>
      </el-row>
      <el-table
        :data="tableData"
        style="width: 100%" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          width="180">
        </el-table-column>
        <el-table-column
          label="人员类型">
          <template slot-scope="scope">{{scope.row.work.name}}</template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.current_page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
    <!-- 添加用户的对话框 -->
    <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
      <!-- 内容主体 -->
      <el-form
        :model="addUserForm"
        ref="addUserFormRef"
        :rules="addUserFormRules"
        label-width="100px"
      >
        <el-form-item label="人员姓名" prop="name">
          <el-input v-model="addUserForm.name"></el-input>
        </el-form-item>
        <el-form-item label="人员类型" prop="type_id">
          <el-select v-model="addUserForm.type_id" placeholder="请选择人员类型">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户的对话框 -->
    <el-dialog
      title="修改用户信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <!-- 内容主体 -->
      <el-form
        :model="editUserForm"
        ref="editUserFormRef"
        label-width="70px"
      >
        <el-form-item label="人员姓名">
          <el-input v-model="editUserForm.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="人员类型" prop="type_id">
          <el-select v-model="editUserForm.type_id" placeholder="请选择人员类型">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Member",
  created() {
    this.getList()
    this.getType()
  },
  data() {
    return {
      queryInfo: {
        type_id: '',
        name: '',
        current_page: 1,
        pagesize: 10
      },
      tableData: [],
      total: 0,
      options: [],
      value: '',
      // 添加用户对话框
      addDialogVisible: false,
      // 修改用户
      editDialogVisible: false,
      editUserForm: {},
      // 用户添加
      addUserForm: {
        name: '',
        type_id: ''
      },
      // 用户添加表单验证规则
      addUserFormRules: {
        name: [
          {required: true, message: '请输入人员姓名', trigger: 'blur'},
          {
            min: 2,
            max: 10,
            message: '用户名的长度在2～10个字',
            trigger: 'blur'
          }
        ],
        type_id: [
          {required: true, message: '请选择人员类型', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    // 监听修改用户对话框的关闭事件
    editDialogClosed () {
      this.$refs.editUserFormRef.resetFields()
    },
    // 修改用户信息
    editUser () {
      // 提交请求前，表单预验证
      this.$refs.editUserFormRef.validate(async valid => {
        // 表单预校验失败
        if (!valid) return
        const { data: res } = await this.$http.put(
          'memberEdit',
          this.editUserForm
        )
        if (res.meta.status !== 200) {
          this.$message.error('更新用户信息失败！')
        }
        // 隐藏添加用户对话框
        this.editDialogVisible = false
        this.$message.success('更新用户信息成功！')
        await this.getList()
      })
    },
    // 编辑用户信息
    async showEditDialog (id) {
      const { data: res } = await this.$http.get('memberShow/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('查询人员信息失败！')
      }
      this.editUserForm = res.data
      this.editDialogVisible = true
    },
    // 监听 添加用户对话框的关闭事件
    addDialogClosed () {
      this.$refs.addUserFormRef.resetFields()
    },
    // 添加用户
    addUser () {
      // 提交请求前，表单预验证
      this.$refs.addUserFormRef.validate(async valid => {
        // 表单预校验失败
        if (!valid) return
        const { data: res } = await this.$http.post('memberAdd', this.addUserForm)
        if (res.meta.status !== 200) {
          this.$message.error('添加人员失败！')
        }
        this.$message.success('添加人员成功！')
        // 隐藏添加用户对话框
        this.addDialogVisible = false
        await this.getList()
      })
    },
    getListByType(id) {
      this.queryInfo.type_id = id;
      this.queryInfo.current_page = 1;
      this.getList()
    },
    async getType() {
      const {data: res} = await this.$http.get('typeList');
      if (res.meta.status !== 200) {
        return this.$message.error('获取人员列表失败！')
      }
      this.options = res.data;
    },
    async getList() {
      const { data: res } = await this.$http.get('memberList', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取人员列表失败！')
      }
      this.tableData = res.data.data
      this.total = res.data.total
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getList()
    },
    handleCurrentChange (newSize) {
      this.queryInfo.current_page = newSize
      this.getList()
    },
    async removeById (id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该人员, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除！')
      }
      const { data: res } = await this.$http.delete('memberDelete/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败！')
      }
      this.$message.success('删除成功！')
      await this.getList()
    }
  }
}
</script>

<style scoped>

</style>
